home *** CD-ROM | disk | FTP | other *** search
/ Aminet 15 / Aminet 15 - Nov 1996.iso / Aminet / dev / misc / libx11.lha / libX11 / fonts.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-09-22  |  13.0 KB  |  454 lines

  1. /* Copyright (c) 1996 by Terje Pedersen.  All Rights Reserved   */
  2. /*                                                              */
  3. /* By using this code you will agree to these terms:            */
  4. /*                                                              */
  5. /* 1. You may not use this code for profit in any way or form   */
  6. /*    unless an agreement with the author has been reached.     */
  7. /*                                                              */
  8. /* 2. The author is not responsible for any damages caused by   */
  9. /*    the use of this code.                                     */
  10. /*                                                              */
  11. /* 3. All modifications are to be released to the public.       */
  12. /*                                                              */
  13. /* Thats it! Have fun!                                          */
  14. /* TP                                                           */
  15. /*                                                              */
  16.  
  17. /***
  18.    NAME
  19.      fonts
  20.    PURPOSE
  21.      add font handling to libX11
  22.    NOTES
  23.      
  24.    HISTORY
  25.      Terje Pedersen - Oct 27, 1994: Created.
  26. ***/
  27.  
  28. #include <intuition/intuition.h>
  29. #include <intuition/intuitionbase.h>
  30.  
  31. #include <graphics/gfx.h>
  32. #include <graphics/gfxbase.h>
  33. #include <graphics/gfxmacros.h>
  34. #include <graphics/displayinfo.h>
  35. #include <graphics/text.h>
  36. #include <devices/timer.h>
  37.  
  38. #include <proto/intuition.h>
  39. #include <proto/graphics.h>
  40. #include <proto/diskfont.h>
  41.  
  42. #include <stdlib.h>
  43. #include <stdio.h>
  44. #include <string.h>
  45. #include <ctype.h>
  46.  
  47. #include "libX11.h"
  48. #include "lists.h"
  49.  
  50. #define XLIB_ILLEGAL_ACCESS 1
  51.  
  52. #include <X11/X.h>
  53. #include <X11/Xlib.h>
  54. #include "amigax_proto.h"
  55. #include "amiga_x.h"
  56.  
  57. extern GC amiga_gc;
  58. extern struct Screen *wb;
  59. extern struct RastPort backrp;
  60. extern int debugxemul;
  61.  
  62. char *X11fontname[20]={"topaz"},*X11fontamiga[20]={"topaz.font"};
  63. int X11fontmapped=1;
  64.  
  65. typedef struct {
  66.   char zName[64];
  67.   int vSize;
  68.   void *pData;
  69. } X11FontNode;
  70.  
  71. ListNode_t *X11FontList=NULL;
  72.  
  73. void X11init_fonts(void){
  74.   FILE *fp;
  75.   char str1[40],str2[40];
  76.   
  77.   fp=fopen("AmigaDefaults","r");
  78.   if(!fp){
  79.     fclose(fp);
  80.     fp=fopen("libx11:AmigaDefaults","r");
  81.   }
  82.   if(!fp){
  83.     fclose(fp);
  84.     return;
  85.   }
  86.   while(!feof(fp)){
  87.     char c;
  88.     fscanf(fp,"%s %s",str1,str2);
  89.     if(str1[0]=='!') while((c=fgetc(fp))!='\n'&&!feof(fp));
  90.     else
  91.     if(strstr(str1,"fontmap")!=NULL){
  92.       char *p=str1,*q;
  93.       while(*p!='.'&&*p!=':'&&*p!=0)p++;
  94.       q=++p;
  95.       while(*q!='.'&&*q!=':'&&*q!=0)q++;
  96.       *q=0;
  97.       X11fontname[X11fontmapped]=malloc(strlen(p)+1);
  98.       if(!X11fontname[X11fontmapped]) X11resource_exit(FONTS3);
  99.       strcpy(X11fontname[X11fontmapped],p);
  100.       X11fontamiga[X11fontmapped]=malloc(strlen(str2)+1);
  101.       if(!X11fontamiga[X11fontmapped]) X11resource_exit(FONTS4);
  102.       strcpy(X11fontamiga[X11fontmapped++],str2);
  103.     }
  104.   }
  105.   fclose(fp);
  106.   X11FontList=List_MakeNull();
  107. }
  108.  
  109. void X11exit_fonts(void){
  110.   int i;
  111.   for(i=1;i<X11fontmapped;i++){
  112.     free(X11fontname[i]);
  113.     free(X11fontamiga[i]);
  114.   }
  115.   X11fontmapped=1;
  116.   List_FreeList(X11FontList);
  117. }
  118.  
  119. int X11FontSize;
  120. char X11FontName[256];
  121. int nFontAccess=0;
  122.  
  123. Font XLoadFont(display, name)
  124.      Display *display;
  125.      char *name;
  126. {
  127.   FILE *fp;
  128.   char str1[128],str2[128],fontname[64];
  129.   UBYTE *fontdata;
  130.   ULONG *fontloc;
  131.   struct TextFont *tfont;
  132.   struct TextAttr *tattr;
  133.   int s1,X11FontSize,s3,s4,numchars,i,width,height,perline,k,l;
  134.   int ascent,descent;
  135. #ifdef DEBUGXEMUL_ENTRY
  136.   printf("XLoadFont [%s] %d\n",name,nFontAccess);
  137. #endif
  138.   nFontAccess++;
  139.  
  140.   strcpy(X11FontName,"libx11:");
  141.   strcat(X11FontName,name);
  142.   strcat(X11FontName,".bdf");
  143.   fp=fopen(X11FontName,"r+");
  144.   if(!fp){
  145.     fclose(fp);
  146.     fp=fopen(X11FontName,"r+");
  147.   }
  148.   if(!fp){ /* 10x20 */
  149.     int style=0;
  150.     fclose(fp);
  151.     if(strstr(name,"-i-")!=NULL) style=FSF_ITALIC;
  152.     if(strstr(name,"-bold-")!=NULL) style|=FSF_BOLD;
  153.     {
  154.       char *c=name;
  155.       X11FontSize=8;
  156.       while(isdigit(*c)&&*c!=0) c++;
  157.       if(*c=='x' && c!=name) sscanf(c+1,"%d",&X11FontSize);
  158.       else{
  159.     while(!isdigit(*c)&&*c!=0) c++;
  160.     sscanf(c,"%d",&X11FontSize);
  161.       }
  162.     }
  163.     if(wb&&wb->RastPort.Font)
  164.       strcpy(X11FontName,wb->RastPort.Font->tf_Message.mn_Node.ln_Name);
  165.     else strcpy(X11FontName,"topaz.font"); /* "helvetica.font" */
  166.     for(i=0;i<X11fontmapped;i++)
  167.       if(strstr(name,X11fontname[i])!=NULL) strcpy(X11FontName,X11fontamiga[i]);
  168.     tattr=(struct TextAttr*)malloc(sizeof(struct TextAttr));
  169.     if(!tattr) X11resource_exit(FONTS5);
  170.     List_AddEntry(pMemoryList,(void*)tattr);
  171.     tattr->ta_Name=X11FontName;
  172.     if(debugxemul) printf("opening font %s size %d\n",X11FontName,X11FontSize);
  173.     tattr->ta_YSize=X11FontSize;
  174.     tattr->ta_Style=style;
  175.     tattr->ta_Flags=FPF_DISKFONT;
  176.     tfont=OpenDiskFont(tattr);
  177.  
  178.     if(!tfont){
  179.       printf("font not found: %s size %d\n",X11FontName,X11FontSize);
  180.       return NULL;
  181.     }
  182.     {
  183.       sFont *sf=malloc(sizeof(sFont));
  184.       List_AddEntry(pMemoryList,(void*)sf);
  185.       if(!sf) X11resource_exit(FONTS6);
  186.       sf->tfont=tfont;
  187.       sf->tattr=tattr;
  188.       return((Font)sf);
  189.     }
  190.   }
  191.   fgets(X11FontName,256,fp);
  192.   fgets(X11FontName,256,fp); sscanf(X11FontName,"%s %s",str1,fontname);
  193.   if(strcmp(str1,"FONT")!=0){
  194.     fgets(X11FontName,256,fp); sscanf(X11FontName,"%s %s",str1,fontname);
  195.   }
  196. /*  printf("font [%s]\n",fontname);*/
  197.   
  198.   fgets(X11FontName,256,fp); sscanf(X11FontName,"%s %d %d %d",str1,&s1,&X11FontSize,&s3);
  199.   fgets(X11FontName,256,fp); sscanf(X11FontName,"%s %d %d %d %d",str1,&width,&height,&s3,&s4);
  200. /*  printf("fontsize [%d %d]\n",width,height);*/
  201.   perline=(width+7)>>3;
  202.   fgets(X11FontName,256,fp);
  203.   fgets(X11FontName,256,fp); sscanf(X11FontName,"%s %d",str1,&descent);
  204.   fgets(X11FontName,256,fp); sscanf(X11FontName,"%s %d",str1,&ascent);
  205.   fgets(X11FontName,256,fp);
  206.   fgets(X11FontName,256,fp); sscanf(X11FontName,"%s %d",str1,&numchars);
  207. /*  printf("number of chars %d\n",numchars);*/
  208.   fontdata=malloc(perline*height*256);
  209.   fontloc=malloc(256*sizeof(ULONG));
  210.   tfont=(struct TextFont*)calloc(sizeof(struct TextFont),1);
  211.   if(!fontdata||!fontloc||!tfont){
  212.     fclose(fp);
  213.     X11resource_exit(FONTS7);    
  214.   }
  215.   List_AddEntry(pMemoryList,(void*)tfont);
  216.   tfont->tf_Message.mn_Node.ln_Name=fontname;
  217.   tfont->tf_YSize=height;
  218.   tfont->tf_XSize=width;
  219.   tfont->tf_LoChar=0;
  220.   tfont->tf_HiChar=255;
  221.   tfont->tf_CharData=fontdata;
  222.   tfont->tf_CharLoc=fontloc;
  223.   tfont->tf_Modulo=perline*256;
  224.   tfont->tf_Flags=42;
  225.   tfont->tf_BoldSmear=1;
  226.   tfont->tf_Baseline=ascent;
  227.   for(i=0;i<numchars;i++){
  228.     int encode;
  229.     fgets(X11FontName,256,fp); sscanf(X11FontName,"%s %s",str1,str2);
  230. /*    printf("id [%s]\n",str2);*/
  231.     fgets(X11FontName,256,fp); sscanf(X11FontName,"%s %d",str1,&encode);
  232.     fgets(X11FontName,256,fp); sscanf(X11FontName,"%s %d %d",str1,&s1,&X11FontSize);
  233.     fgets(X11FontName,256,fp); sscanf(X11FontName,"%s %d %d",str1,&s1,&X11FontSize);
  234.     fgets(X11FontName,256,fp); sscanf(X11FontName,"%s %d %d %d %d",str1,&s1,&X11FontSize,&s3,&s4);
  235.     fgets(X11FontName,256,fp);
  236.     *(((UWORD*)fontloc)+encode*2)=(UWORD)(encode*(((width+7)>>3)<<3));
  237.     *(((UWORD*)fontloc)+encode*2+1)=(UWORD)(width);
  238.     
  239.     for(l=0;l<height;l++){
  240.       int val;
  241.       char vstr[3];
  242.       vstr[2]=0;
  243.       fgets(X11FontName,256,fp);
  244.       for(k=0;k<perline;k++){
  245.     vstr[0]=X11FontName[k*2];
  246.     vstr[1]=X11FontName[k*2+1];
  247.     sscanf(vstr,"%x",&val);
  248.     *(fontdata+l*perline*256+encode*perline+k)=(UBYTE)val;
  249.       }
  250.     }
  251.     fgets(X11FontName,256,fp);
  252.   }
  253.  
  254.   fclose(fp);
  255.   return((Font)tfont);
  256. }
  257.  
  258. XFontStruct *XLoadQueryFont(Display *display, char *name)
  259. {
  260.   XFontStruct*     xfont=calloc(sizeof(XFontStruct),1);
  261.   struct TextFont* tf;
  262.   boolean          bGotData=False;
  263.   X11FontNode*     fnode;
  264.  
  265. #ifdef DEBUGXEMUL_ENTRY
  266.   printf("XLoadQueryFont [%s]\n",name);
  267. #endif
  268.   List_AddEntry(pMemoryList,(void*)xfont);
  269.   if(!name) return(NULL);
  270.   if(!xfont) X11resource_exit(FONTS2);
  271.   if(strcmp(name,"fixed")!=0){
  272.     int style=0;
  273.     xfont->fid=XLoadFont(display,name);
  274.     if(strstr(name,"-i-")!=NULL) style=FSF_ITALIC;
  275.     if(strstr(name,"-bold-")!=NULL) style|=FSF_BOLD;
  276.   }
  277.   else{
  278.     char str[10];
  279.     sprintf(str,"10x%d",wb->RastPort.Font->tf_YSize);
  280.     xfont->fid=XLoadFont(display,str) /*amiga_gc->values.font*/;
  281.   }
  282.   {
  283.     ListNode_t *pNode=X11FontList;
  284.     while(pNode!=NULL && pNode->pData!=NULL){
  285.       fnode=(X11FontNode*)pNode->pData;
  286.       if(strcmp(fnode->zName,X11FontName)==0 && fnode->vSize==X11FontSize){
  287.     bGotData=True;
  288.     break;
  289.       }
  290.       List_GetNext(&pNode);
  291.     }
  292.   }
  293.   if(xfont->fid){
  294.     int i;
  295.     tf=(struct TextFont *)((sFont*)xfont->fid)->tfont;
  296.     xfont->min_char_or_byte2=tf->tf_LoChar;
  297.     xfont->max_char_or_byte2=tf->tf_HiChar;
  298.     xfont->max_bounds.width=tf->tf_XSize;
  299.     xfont->max_bounds.ascent=tf->tf_Baseline;
  300.     xfont->max_bounds.descent=tf->tf_YSize-tf->tf_Baseline;
  301.     xfont->min_bounds.lbearing=1;
  302.     xfont->min_bounds.rbearing=tf->tf_XSize-1;
  303.     xfont->min_bounds.width=tf->tf_XSize;
  304.     xfont->min_bounds.ascent=tf->tf_Baseline;
  305.     xfont->min_bounds.descent=tf->tf_YSize-tf->tf_Baseline-1;
  306.     xfont->ascent=xfont->max_bounds.ascent;
  307.     if(!bGotData){
  308.       xfont->per_char=malloc((tf->tf_HiChar-tf->tf_LoChar)*sizeof(XCharStruct));
  309.       if(!xfont->per_char) X11resource_exit(FONTS2);
  310.       List_AddEntry(pMemoryList,(void*)xfont->per_char);
  311.       for(i=0;i<(tf->tf_HiChar-tf->tf_LoChar);i++){
  312.     char c=i+tf->tf_LoChar;
  313.     xfont->per_char[i].width=XTextWidth(xfont,&c,1);
  314.       }
  315.  
  316.       fnode=malloc(sizeof(X11FontNode));
  317.       if(!fnode) X11resource_exit(FONTS8);
  318.       strcpy(fnode->zName,X11FontName);
  319.       fnode->vSize=X11FontSize;
  320.       fnode->pData=xfont->per_char;
  321.       List_AddEntry(X11FontList,(void*)fnode);
  322.     } else
  323.       xfont->per_char=fnode->pData;
  324.     xfont->descent=xfont->max_bounds.descent;
  325. /*    printf(" font info width %d ascent %d descent %d\n",xfont->max_bounds.width,xfont->max_bounds.ascent,xfont->max_bounds.descent);*/
  326.     return(xfont);
  327.   }
  328.   free(xfont);
  329.   return(NULL);
  330. }
  331.  
  332. XmFontListCreate(){/*        File 'statuswin.o'*/
  333. #if (DEBUGXEMUL_ENTRY) || (DEBUGXEMUL_WARNING)
  334.   printf("WARNING: XmFontListCreate\n");
  335. #endif
  336.   return(0);
  337. }
  338.  
  339. XFreeFont(display, font_struct)
  340.      Display *display;
  341.      XFontStruct *font_struct;
  342. {
  343.   struct TextFont *f=(struct TextFont*)(((sFont*)font_struct->fid)->tfont);
  344. #ifdef DEBUGXEMUL_ENTRY
  345.   printf("XFreeFont %d\n",nFontAccess);
  346.   printf("XFreeFont\n");
  347. #endif
  348.   nFontAccess--;
  349.   if(!f){
  350.     printf("error freeing font!\n");
  351.     exit(-1);
  352.   }
  353.   if(((struct TextFont*)((sFont*)font_struct->fid)->tfont)->tf_Flags==42){
  354.     free(f->tf_CharData);
  355.     free(f->tf_CharLoc);
  356.     List_RemoveEntry(pMemoryList,(void*)f);
  357.   }else{
  358.     if(font_struct->fid!=amiga_gc->values.font)
  359.       CloseFont((struct TextFont*)(((sFont*)font_struct->fid)->tfont));
  360.     List_RemoveEntry(pMemoryList,(void*)((sFont*)font_struct->fid)->tattr);
  361.     List_RemoveEntry(pMemoryList,(void*)font_struct->fid);
  362.   }
  363.   List_RemoveEntry(pMemoryList,(void*)font_struct->per_char);
  364.   List_RemoveEntry(pMemoryList,(void*)font_struct);
  365.   return(0);
  366. }
  367.  
  368. XSetFont(display, gc, font)
  369.      Display *display;
  370.      GC gc;
  371.      Font font;
  372. {/*                File 'class1.o'*/
  373. #ifdef DEBUGXEMUL_ENTRY
  374.   printf("XSetFont\n");
  375. #endif
  376.   gc->values.font=font;
  377.   return(0);
  378. }
  379.  
  380. XTextExtents(font_struct, string, nchars, direction_return,
  381.          font_ascent_return, font_descent_return, overall)
  382.      XFontStruct *font_struct;
  383.      char *string;
  384.      int nchars;
  385.      int *direction_return;
  386.      int *font_ascent_return, *font_descent_return;
  387.      XCharStruct *overall;
  388. {
  389. /*
  390.   struct TextFont *tf=((sFont*)font_struct->fid)->tfont;
  391.   int len=0,i;
  392. */
  393. #ifdef DEBUGXEMUL
  394.   printf("XTextExtents\n");
  395. #endif
  396. /*  for(i=0;i<nchars;i++){
  397.     char *addr=(char*)tf->tf_CharLoc+(string[i]-32)*4;
  398.     len+=(*(unsigned long*)addr)&0xff;
  399.   }*/
  400.   overall->rbearing=XTextWidth(font_struct,string,nchars);
  401.   overall->ascent=font_struct->max_bounds.ascent;
  402.   overall->descent=font_struct->max_bounds.descent;
  403. /*  overall->rbearing=font_struct->max_bounds.rbearing;*/
  404.   overall->lbearing=font_struct->max_bounds.lbearing;
  405.   overall->width=overall->rbearing;
  406.  
  407.   return(0);
  408. }
  409.  
  410. int XTextWidth(font_struct, string, count)
  411.      XFontStruct *font_struct;
  412.      char *string;
  413.      int count;
  414. {
  415.   struct TextFont *tf=((sFont*)font_struct->fid)->tfont;
  416.   int len=0;
  417.   struct RastPort *drp;
  418. #ifdef DEBUGXEMUL_ENTRY
  419.   printf("XTextWidth\n");
  420. #endif
  421.   X11testback(20,20,1);
  422.   drp=&backrp;
  423. /*  for(i=0;i<count;i++){
  424.     char *addr=(char*)tf->tf_CharLoc+(string[i]-32)*4;
  425.     len+=(*(unsigned long*)addr)&0xff;
  426.   }*/
  427.   SetFont(drp,tf);
  428.   len=TextLength(drp,string,count)/*font_struct->min_bounds.rbearing*strlen(string) /*+strlen(string)*/*/;
  429.   return(len);
  430. }
  431.  
  432. char **XListFonts(display, pattern, maxnames, actual_count_return)
  433.      Display *display;
  434.      char *pattern;
  435.      int maxnames;
  436.      int *actual_count_return;
  437. {/*              File 'w_drawprim.o'*/
  438. #ifdef DEBUGXEMUL_ENTRY
  439.   printf("XListFonts\n");
  440. #endif
  441.   *actual_count_return=X11fontmapped;
  442.   return(X11fontname);
  443. }
  444.  
  445. XFontStruct *XQueryFont(display, font_ID)
  446.      Display *display;
  447.      XID font_ID;
  448. {
  449. #if (DEBUGXEMUL_ENTRY) || (DEBUGXEMUL_WARNING)
  450.   printf("WARNING: XQueryFont\n");
  451. #endif
  452.   return (XFontStruct*)NULL;
  453. }
  454.